package com.mashibing.juc.c_026_01_ThreadPool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Callable通常是配合线程池使用.
 * Callable是一个任务（有返回值的Runnable)
 * @author: Yuanzt
 * @Date: 2021/4/2
 */
public class T03_Callable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String> c = new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello callable";
            }
        };
        ExecutorService service = Executors.newCachedThreadPool();
        /*Future<String> future = service.submit(()->"Hello callable");//异步*/
        Future<String> future = service.submit(c);//异步
        System.out.println(future.get());//阻塞，获取submit方法的返回值
        service.shutdown();
    }
}
